package com.jinzihan.www.service;

import com.jinzihan.www.entity.Commodity;
import com.jinzihan.www.entity.Node;
import com.jinzihan.www.util.CMUtility;

import java.util.Scanner;

/**
 * @author jin-zihan
 * @create 2021-11-26-16:11
 */
public class CommodityService {
    static Node head = null;

    public void AddNode(Commodity commodity) {
        Node newNode = new Node(commodity);
        if (head == null) {
            head = newNode;
            return;
        }
        Node tmp = head;
        while (tmp.next != null) {
            tmp = tmp.next;
        }
        tmp.next = newNode;
    }

    public Node searchNodeLocation(int index) {
        if (index >= 0 && index <= getLength() - 1) {
            Node tempNode = head;
            for (int i = 0; i < index - 1; i++) tempNode = tempNode.next;
            return tempNode;
        } else {
            return null;
        }
    }

    public static boolean deleteNode(int index) {
        if (index < 1 || index > getLength()) {
            return false;
        }
        if (index == 1) {
            head = head.next;
            return true;
        }
        int i = 1;
        Node preNode = head;
        Node curNode = preNode.next;
        while (curNode != null) {
            if (i == index) {
                preNode.next = curNode.next;
                return true;
            }
            preNode = curNode;
            curNode = curNode.next;
            i++;
        }
        return false;
    }

    public static int getLength() {
        int length = 0;
        Node tmp = head;
        while (tmp != null) {
            length++;
            tmp = tmp.next;
        }
        return length;
    }

    public static void printList() {
        Node tmp = head;
        while (tmp != null) {
            System.out.print(tmp.commodity.toString() + "\n");
            tmp = tmp.next;
        }
    }

    public static void printAllCommodity() {
        System.out.println("-----------------------------秘籍列表--------------------------------");
        if (getLength() == 0) {
            System.out.println("没有秘籍");
        } else {
            System.out.println("ID\t\t\t状态\t\t\t名称\t\t\t价格\t\t\t描述\t\t\t等级");
            printList();
        }
    }


    public static void deleteCommodity(CommodityService commodityList) {
        System.out.println("------------------------------删除秘籍---------------------------------");
        System.out.println("请选择删除的编号（输入-1退出）：");
        Node node;
        int number;
        for (; ; ) {
            number = CMUtility.readInt();
            if (number == -1) {
                return;
            }
            node = commodityList.searchNodeLocation(number - 1);
            if (node == null) {
                System.out.println("无法找到指定秘籍！");
            } else {
                break;
            }
        }
        System.out.println("确认是否删除（Y/N）:");
        char isDelete = CMUtility.readConfirmSelection();
        if (isDelete == 'Y') {
            boolean deleteSuccess = CommodityService.deleteNode(number - 1);
            if (deleteSuccess) {
                System.out.println("---------------------删除完成-------------------------");
            } else {
                System.out.println("---------------------删除失败-------------------------");
            }
        }
    }

    public static void ModifyCommodity(CommodityService commodityList) {
        System.out.println("----------------------------修改商品---------------------------------");
        System.out.println("请选择修改的编号（输入-1退出）：");
        Node node;
        int number;
        for (; ; ) {
            number = CMUtility.readInt();
            if (number == -1) {
                return;
            }
            node = commodityList.searchNodeLocation(number - 1);
            if (node == null) {
                System.out.println("无法找到指定秘籍！");
            } else {
                break;
            }
        }
        System.out.println("请输入修改后的数据：");
        System.out.println("名字：");
        node.commodity.name = CMUtility.readString(10, node.commodity.name);
        System.out.println("价格：");
        node.commodity.setPrice(CMUtility.readInt(node.commodity.getPrice()));
        System.out.println("描述：");
        node.commodity.introduction = CMUtility.readString(30, node.commodity.introduction);
        System.out.println("等级：");
        node.commodity.level = CMUtility.readString(10, node.commodity.level);
    }

    public static void addCommodity(CommodityService commodityList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品的信息：");
        System.out.println("名字：");
        String name = sc.nextLine();
        System.out.println("价格：");
        int price = sc.nextInt();
        System.out.println("描述：");
        String description = sc.next();
        System.out.println("等级：");
        String level = sc.next();
        Commodity commodity = new Commodity(CommodityService.getLength() + 1, name, price, description, level);
        commodityList.AddNode(commodity);
    }
}
